<template>
  <div class="add-enterprise">
    <header class="add-header">
      <div class="left">
        <span class="arrow" @click="$router.back()"><i class="el-icon-arrow-left" />返回</span>
        <span>|</span>
        <span>{{ id?"编辑企业":"添加企业" }}</span>
      </div>
      <div class="right">
        黑马程序员
      </div>
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">企业信息</div>
        <div class="form">
          <el-form ref="ruleForm" :model="addForm" :rules="addRules" label-width="100px">
            <el-form-item label="企业名称" prop="name">
              <el-input v-model="addForm.name" />
            </el-form-item>
            <el-form-item label="法人" prop="legalPerson">
              <el-input v-model="addForm.legalPerson" />
            </el-form-item>
            <el-form-item label="注册地址" prop="registeredAddress">
              <el-input v-model="addForm.registeredAddress" />
            </el-form-item>
            <el-form-item label="所在行业" prop="industryCode">
              <el-select v-model="addForm.industryCode" @focus="getIndustry">
                <el-option v-for="item in selectIndustry" :key="item.industryCode" :value="item.industryCode" :label="item.industryName" />
              </el-select>
            </el-form-item>
            <el-form-item label="企业联系人" prop="contact">
              <el-input v-model="addForm.contact" />
            </el-form-item>
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="addForm.contactNumber" />
            </el-form-item>
            <el-form-item label="营业执照" prop="besinessLicenseId">
              <el-upload

                class="upload-demo"
                action="#"
                :http-request="upload"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传png/docx文件，且不超过5M</div>
              </el-upload>
              <img class="img" :src="addForm.businessLicenseUrl" alt="">
            </el-form-item>
          </el-form>
        </div>
      </div>
    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button>重置</el-button>
        <el-button type="primary" @click="submitEnterPrise">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { getListIndustryAPI, UploadEnterPriseAPI, ShowEnterPriseAPI, EditEnterPriseAPI } from '@/api/park'
import { UploadAPI } from '@/api/commont'
export default {
  data() {
    return {
      addForm: {
        name: '', // 企业名称
        legalPerson: '', // 法人
        registeredAddress: '', // 注册地址
        industryCode: '', // 所在行业
        contact: '', // 企业联系人
        contactNumber: '', // 联系人电话
        businessLicenseUrl: '', // 营业执照url
        businessLicenseId: '' // 营业执照id
      },
      addRules: {
        name: [
          { required: true, message: '企业名称为必填', trigger: 'blur' }
        ],
        legalPerson: [
          { required: true, message: '法人为必填', trigger: 'blur' }
        ],
        registeredAddress: [
          { required: true, message: '注册地址为必填', trigger: 'blur' }
        ],
        industryCode: [
          { required: true, message: '所在行业为必填', trigger: 'change' }
        ],
        contact: [
          { required: true, message: '企业联系人为必填', trigger: 'blur' }
        ],
        contactNumber: [
          { required: true, message: '企业联系人电话为必填', trigger: 'blur' },
          { pattern: /^1[3,5,7,8,9]\d{9}$/, message: '电话号码必须是11位', trigger: 'blur' }
        ],
        businessLicenseId: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ]
      },
      selectIndustry: []
    }
  },
  computed: {
    id() {
      return this.$route.query.id
    }
  },
  created() {
    this.getIndustryList()
    if (this.$route.query.id) {
      this.showEnterPriseDetail()
    }
  },
  methods: {
    // 获取详细工作信息
    async getIndustryList() {
      const res = await getListIndustryAPI()
      console.log(res)
      this.selectIndustry = res.data
    },
    // 获取焦点时获取详细工作信息
    getIndustry() {
      this.getIndustryList()
    },
    async upload(res) {
      // file:要上传给后端接口的文件对象
      const { file } = res
      // 1.上传文件准备一个FormDATA
      const fd = new FormData()
      fd.append('file', file)
      fd.append('type', 'businessLicense')
      console.log(6666)
      // 2.调用接口
      const _res = await UploadAPI(fd)
      this.addForm.businessLicenseUrl = _res.data.url
      this.addForm.businessLicenseId = _res.data.id
      // 上传之后对阴营业执照id做校验
      this.$refs.ruleForm.validateField('businessLicenseId')
    },
    // beforeAvatarUpload(file) {
    //   const isPng = file.type === 'image/png/docx/doc/jpg'
    //   const isLt5M = file.size / 1024 / 1024 < 5

    //   if (!isPng) {
    //     this.$message.error('上传头像图片只能是 png/docx 格式!')
    //   }
    //   if (!isLt5M) {
    //     this.$message.error('上传头像图片大小不能超过 5MB!')
    //   }
    //   return isPng && isLt5M
    // },
    // 提交企业信息数据
    submitEnterPrise() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          // 先判断是提交还是修改
          if (this.id) {
            // 修改表单
            await EditEnterPriseAPI(this.addForm)
          } else {
            // 提交表单数据
            await UploadEnterPriseAPI(this.addForm)
          }
          // 弹出框
          this.$message.success(`${this.id ? '修改' : '添加'}添加成功`)
          // 返回界面
          this.$router.back()
        }
      })
    },
    // 调用企业接口进行回显
    async showEnterPriseDetail() {
      const res = await ShowEnterPriseAPI(this.id)
      console.log(res)
      const { id, name, legalPerson, registeredAddress, industryCode, contact, contactNumber, businessLicenseUrl, businessLicenseId } = res.data
      this.addForm = { id, name, legalPerson, registeredAddress, industryCode, contact, contactNumber, businessLicenseUrl, businessLicenseId }
    }
  }
}
</script>

<style scoped lang="scss">
.add-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    justify-content: space-between;
    padding: 0 20px;
    height: 64px;
    line-height: 64px;
    font-size: 16px;
    font-weight: bold;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }
      .arrow{
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      .title {
        height: 60px;
        line-height: 60px;
        padding-left:20px;
      }
      .form {
        margin-bottom: 20px;
        padding: 20px 65px 24px;
        .el-form{
          display: flex;
          flex-wrap: wrap;
          .el-form-item{
            width: 50%;
          }
        }
      }
    }
  }

  .add-footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 24px 50px;
    color: #000000d9;
    font-size: 14px;
    background: #fff;
    text-align: center;
  }
}
/* 直接针对类名img的预览图 */
.img {
  /* 避免拉伸变形，宽高自适应容器 */
  width: 50%;
  height: auto;
  /* 柔和圆角，让边角不生硬 */
  border-radius: 8px;
  /* 加层 subtle 阴影，立体但不夸张 */
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  /* 过渡动画，让hover效果更丝滑 */
  transition: all 0.3s ease;
  /* 隐藏溢出内容（配合圆角裁剪） */
  overflow: hidden;
  /* 让图片作为块级元素，消除底部空隙 */
  display: block;
}

/* 悬停时轻微放大 + 阴影增强，提升交互感 */
.img:hover {
  /* 轻微放大，突出交互 */
  transform: scale(1.03);
  /* 阴影更明显，强化悬浮感 */
  box-shadow: 0 4px 12px rgba(0,0,0,0.12);
}
</style>
